//
//  KYSignController.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/16.
//  Copyright © 2017年 ???. All rights reserved.
//

#import "KYSignController.h"
#import "KYCustomCheckInView.h"
#import "KYCustomPlaningView.h"
#import "KYCalendarViewController.h"
#import "KYCheckInNetDateModel.h"
#import "KYAdViewController.h"
#import "KYServer.h"
#import "KYAdViewController.h"
#import "KYPrizeDrawView.h"
#import "KYCheckInPrizeDetialModel.h"
#import "KYCheckInOverallDataModel.h"
#import "KYCacheManager.h"

@interface KYSignController ()<KYCustomCheckInViewDelegate,KYPrizeDrawViewDelegate>
/**
 *  日历控制器
 */
@property (nonatomic, strong) KYCalendarViewController *calendarVC;
@property (nonatomic, weak) KYCustomCheckInView *checkView;
@property (nonatomic, weak) KYPrizeDrawView *prizeDrawView;
@property (nonatomic, weak) UITableView *tableView;
@end

@implementation KYSignController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpSubViews];
}

- (void)setUpSubViews {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.title = @"签到抽奖";
    // 2.创建签到view
    KYCustomCheckInView *checkView = [KYCustomCheckInView customCheckInView];
    checkView.frame = CGRectMake(0,64, self.view.frame.size.width, 250);
    checkView.delegate = self;
    [self.view addSubview:checkView];
    self.checkView = checkView;
    // 3.创建抽奖界面
    KYPrizeDrawView *prizeDrawView = [[KYPrizeDrawView alloc] init];
    prizeDrawView.frame = CGRectMake(0, 314, self.view.frame.size.width, 270);
    [self.view addSubview:prizeDrawView];
    self.prizeDrawView = prizeDrawView;
    self.prizeDrawView.delegate = self;
    
    [self setUpBaseData];
}
- (void)setUpBaseData{
    //获取网络数据缓存
    KYCacheManager *cacheMgr = [KYCacheManager shareInstance];
    if([cacheMgr ky_isContainObjectForKey:KYPrizeData]){
        NSArray *dataArr = (NSArray *)[cacheMgr ky_ObjectForKey:KYPrizeData];
        KYCheckInOverallDataModel *model;
        NSString *CheckDataCacheKey = [NSString stringWithFormat:@"KYPrize%@",[NSDate currentDayString]];
        if ([cacheMgr ky_isContainObjectForKey:CheckDataCacheKey]) {
            model = (KYCheckInOverallDataModel *)[cacheMgr ky_ObjectForKey:CheckDataCacheKey];
        }else{
            model = (KYCheckInOverallDataModel *)[cacheMgr ky_ObjectForKey:KYPrizeOverallData];
            model.retainDrawNumber = 0;
        }
        [self.prizeDrawView refreshPrizeDrawDataWithDataArr:dataArr prizeDrawOverallData:model];
    }
}
#pragma mark - KYCustomCheckInViewDelegate
- (void)customCheckInView:(KYCustomCheckInView *)checkInView didClickBtnWithType:(KYCustomCheckInViewBtnsType)type ButtonStatus:(BOOL)isSelected
{
    switch (type) {
        case KYCustomCheckInDetailBtn: // 展示日历
            if (isSelected) {
                [self showDetailView];
            }else{
                [self hideDetailView];
            }
            break;
            
        case KYCustomCheckInPlaningBtn: // 展示攻略
            [self showPlaningView];
            break;
            
        default:
            break;
    }
}

- (void)adviewClick{
    KYAdViewController *adVC = [[KYAdViewController alloc] init];
    adVC.urlStr = [KYCheckInNetDateModel getCacheData].checkInImageJumpUrl;
    [KYServer postAdViewClickEvent:@"click" DataSuccess:nil failure:nil];
    [self.navigationController pushViewController:adVC animated:YES];
}

//重要方法，数据传递到这里
- (void)refreshPrizeDrawWithDataArray:(NSArray *)arr overallPrizeDrawModel:(KYCheckInOverallDataModel *)model{
    //将数据传递给底部抽奖栏，让抽奖按钮可用
    [self.prizeDrawView refreshPrizeDrawDataWithDataArr:arr prizeDrawOverallData:model];
    [self.checkView refreshFooter];
}
- (void)updateTheDrawNumberWith:(NSInteger)time{
    [self.checkView updateDrawNumber:time];
}

//展示用户攻略
- (void)showPlaningView
{
    KYCustomPlaningView *planingView = [KYCustomPlaningView customPlaningView];
    planingView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [planingView show];
}
- (void)adClickWithModel:(KYCheckInPrizeDetialModel *)model{
    KYAdViewController *adVC = [[KYAdViewController alloc] init];
    adVC.urlStr = model.apkurl;
    [KYServer postAdViewClickEvent:@"click" urlString:model.url DataSuccess:^(NSDictionary *dataDict) {
    } failure:nil];
     [self.navigationController pushViewController:adVC animated:YES];
}

#warning - 这里加载日历展示界面
- (void)showDetailView
{
    [self.view addSubview:self.calendarVC.view];
    self.calendarVC.view.frame = CGRectMake(0, -self.calendarVC.view.ky_height, [UIScreen mainScreen].bounds.size.width, self.calendarVC.view.ky_height);
    [UIView animateWithDuration:1.0 animations:^{
        self.calendarVC.view.frame = CGRectMake(0, 195, [UIScreen mainScreen].bounds.size.width, self.calendarVC.view.ky_height);
    }];
}
- (void)hideDetailView
{
    [UIView animateWithDuration:1.0 animations:^{
        self.calendarVC.view.frame = CGRectMake(0, -self.calendarVC.view.ky_height, [UIScreen mainScreen].bounds.size.width, self.calendarVC.view.ky_height);
    } completion:^(BOOL finished) {
        [self.calendarVC.view removeFromSuperview];
    }];
}
#pragma mark - 懒加载
- (KYCalendarViewController *)calendarVC{
    if (!_calendarVC) {
        _calendarVC = [[KYCalendarViewController alloc] init];
    }
    return _calendarVC;
}

- (void)dealloc
{
    NSLog(@"%s",__func__);
}
@end
